<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="write">
        <h1>
            <strong>标题1</strong></h1>
        <h2>
            <strong>标题2</strong>
        </h2>
        <h2>标题2</h2>
        <h3>标题3</h3>
        <h4>标题4</h4>
        <h5>标题5</h5>
        <h1>标题1</h1>
        <h1>标题1</h1>
    </div>




    <!-- 左侧目录导航栏 ---------------------------------------->
    <div class="sideBar">
        <div class="sidebarText"></div>
        <div class="hideBtn">显示导航</div>
        <div class="author"> power from lisipeng</div>
    </div>
    <style>
        .sidebarText {
            min-width: 16rem;
            max-width: 25rem;
            height: 100vh;
            background-color: #f3f4f4;
            position: fixed;
            left: 0;
            top: 0;
            display: none;
            overflow-x: auto;
            white-space: nowrap;
            padding: 0.8rem 0.8rem 2.5rem 0.8rem;
        }
        
        .hideBtn {
            width: 5rem;
            height: 2rem;
            background-color: #ccc;
            position: fixed;
            left: 0.5rem;
            top: 0.8rem;
            text-align: center;
            color: #595959;
            cursor: pointer;
            line-height: 2rem;
            border-radius: 0.5rem;
            user-select: none;
        }
        
        .author {
            min-width: 16rem;
            max-width: 25rem;
            text-align: center;
            position: fixed;
            left: 0;
            bottom: 0.7rem;
            display: none;
            user-select: none;
        }
        
        .sideH1 {
            margin: 0.2rem 0 0.2rem 0;
            cursor: pointer;
        }
        
        .sideH2 {
            margin: 0.2rem 0 0.2rem 0.6rem;
            cursor: pointer;
        }
        
        .sideH3 {
            margin: 0.2rem 0 0.2rem 1.2rem;
            cursor: pointer;
        }
        
        .sideH4 {
            margin: 0.2rem 0 0.2rem 1.8rem;
            cursor: pointer;
        }
        
        .sideH5 {
            margin: 0.2rem 0 0.2rem 2.4rem;
            cursor: pointer;
        }
        
        .sideH6 {
            margin: 0.2rem 0 0.2rem 3rem;
            cursor: pointer;
        }
    </style>
    <script>
        var hideBtn = document.querySelector(".hideBtn"); //按钮
        var hideMain = document.querySelector(".sidebarText"); //目录内容
        var author = document.querySelector(".author"); //作者署名
        var isHide = true; //隐藏/显示状态
        var hidFun = () => {
                if (isHide) {
                    hideMain.style.display = "none";
                    hideBtn.style.left = "0.5rem";
                    hideBtn.innerHTML = "显示";
                    author.style.display = "none";
                } else {
                    hideMain.style.display = "block";
                    let leftLength = hideMain.offsetWidth / 16 + 0.5;
                    hideBtn.style.left = `${leftLength}rem`;
                    hideBtn.innerHTML = "隐藏";
                    author.style.display = "block";
                }
            }
            // 点击按钮();
        hideBtn.onclick = function() {
            isHide = !isHide;
            hidFun();
        }

        var titleList = document.getElementById("write").childNodes; // 每个段落、标题节点
        //渲染目录子节点
        var list = [];
        titleList.forEach((item, index) => {
            if (
                item.localName == 'h1' || item.localName == 'h2' || item.localName == 'h3' || item.localName == 'h4' || item.localName == 'h5' || item.localName == 'h6'
            ) {
                console.log(item)
                let title = "";
                for (let i = 1; i < item.childNodes.length; i++) {
                    title = title + item.childNodes[i].innerHTML;
                    console.log(item)
                }
                list.push({
                    text: title,
                    a: item.childNodes[0].name,
                    tag: item.nodeName
                })
            }
        })
        console.dir(list)
        list.forEach((item, index) => {
                let sideH1 = document.createElement("div");
                sideH1.setAttribute("class", `side` + `${item.tag}`);
                sideH1.setAttribute("id", `${item.a}`);
                sideH1.innerHTML = item.text;
                hideMain.appendChild(sideH1);
            })
            // 点击事件
        hideMain.onclick = (event) => {
            if (event.target.className != "sidebarText") {
                let jumpArr = document.querySelector(`a[name=` + `"${event.target.id}"` + `]`);
                jumpArr.scrollIntoView();
            }
        }
    </script>
    <!-- 左侧导航栏 end------------------ -->




</body>

</html>